#! /usr/bin/env node
const log = message => console.log(chalk.green(`${message}`))
// 要生成的列的key
let option = {
  name: '',
  pwd: '',
  age: ''
}


var program = require('commander');
const chalk = require('chalk')
const { createView } = require('./auto-file/generateView')
const { createApi } = require('./auto-file/generateApi');
const { createRouter } = require('./auto-file/generateRouter');
const { createTabble } = require('./auto-file/generateTabble');
const { createMock } = require('./auto-file/generateMock');
program
  .parse(process.argv);
let options = {}
program.args.forEach(item => {
  options[item.split('=')[0]] = item.split('=')[1]
})
if (!options.url && !options.name && !options.path) {
  console.log(chalk.red("请执行 node crud name=侧边栏名字 path=路由名字 url=获取列表地址(非必要)  noRouter=不需要的路由(非必要)"))
} else {
  log(`请输入要生成的文件名称、会生成在 option、api、mock(有url不生成)、view的${options.path}目录下`)
  log(`路由会生成在的/src/router/common/modules目录下`)
  process.stdin.on('data', async chunk => {
    const inputName = String(chunk).trim().toString()
    console.log()
    await createView(options.path, inputName)
    await createApi(options.path, inputName)
    if (!options.noRouter) {
      await createRouter(options, inputName)
    }
    if (!options.url) {
      await createTabble(options, [option], inputName)
      await createMock(options, [option], inputName)
    } else {
      // 如果有后台配合表格可直接生成    
      const axios = require('axios')
      let rsp = await axios.get(options.url)
      await createTabble(options, rsp.data.data.list, inputName)
    }
    process.stdin.emit('end')
  })
  process.stdin.on('end', () => {
    log('生成结束')
    process.exit()
  })

}
